package com.cubeofnine.sugar.business;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import com.cubeofnine.sugar.service.element.*;

import android.content.Context;

public class ContactsBusiness extends BusinessImpt {

	public static final String CONTACTS_ORDER_BY_DEFAULT = "first_name, last_name";

	public static String getClassName() {
		return ContactsBusiness.class.getName();
	}

	public ContactsBusiness(Context context) throws Exception {
		super(context, MODULE_CONTACTS);
	}

	public EntryListResult getEntryList(String query, String orderBy,
			int offset, Vector<String> fieldName, int maxResults, int deleted)
			throws Exception {

		if (!query.trim().isEmpty()) {
			query = "LOWER(Contacts.first_name) LIKE '" + query
					+ "%' OR LOWER(Contacts.last_name) LIKE '" + query + "%'";
		}

		EntryListResult contactsEntryList = super.getEntryList(query, orderBy,
				offset, fieldName, maxResults, deleted);

		if (contactsEntryList != null && contactsEntryList.getResultCount() > 0) {
			String accountId = "";
			String accountName = "";
			RelationshipsResult relationshipsResult = null;
			EntryValue contactEntry = null;
			EntryValue entryValue = null;
			EntryResult entryResult = null;

			Map<?, ?> contactsEntryMap = contactsEntryList.getEntryList();
			String contactsEntryKey = "";
			Set<?> contactsEntrySet = contactsEntryMap.keySet();
			Iterator<?> contactsEntryIter = contactsEntrySet.iterator();

			Map<?, ?> entryList = null;
			String key = "";
			Set<?> set = null;
			Iterator<?> iter = null;

			Map<?, ?> nameValueMap = null;
			NameValue nameValue = null;

			while (contactsEntryIter.hasNext()) {
				accountId = "";
				accountName = "";
				contactsEntryKey = (String) contactsEntryIter.next();
				contactEntry = (EntryValue) contactsEntryMap
						.get(contactsEntryKey);

				relationshipsResult = super.getAccountsContacts(contactEntry
						.getId());
				if (relationshipsResult != null
						&& relationshipsResult.getIdMap() != null) {
					entryList = relationshipsResult.getIdMap();
					set = entryList.keySet();
					iter = set.iterator();

					while (iter.hasNext()) {
						key = (String) iter.next();
						IdMod ids = (IdMod) entryList.get(key);
						accountId = ids.getId();
					}
				}

				if (accountId != null && !accountId.isEmpty()) {
					entryResult = super.getEntry(MODULE_ACCOUNTS, accountId,
							null);
					if (entryResult != null) {

						try {
							entryList = entryResult.getEntryList();
							set = entryList.keySet();
							iter = set.iterator();

							// Once Row Only
							while (iter.hasNext()) {
								key = (String) iter.next();
								entryValue = (EntryValue) entryList.get(key);

								nameValueMap = entryValue.getNameValueList();
								nameValue = (NameValue) nameValueMap
										.get("name");
								accountName = nameValue.getValue();
							}
						} catch (Exception e) {

						}
					}
				}

				contactEntry.addNameValue("account_id", new NameValue(
						"account_id", accountId));
				contactEntry.addNameValue("account_name", new NameValue(
						"account_name", accountName));
			}
		}

		return contactsEntryList;
	}

	public EntryListResult getContactList(String query, String orderBy,
			int offset, int maxResults) throws Exception {

		if (!query.trim().isEmpty()) {
			query = "LOWER(c.first_name) LIKE '" + query
					+ "%' OR LOWER(c.last_name) LIKE '" + query + "%'";
		}

		EntryListResult contactsEntryList = getServiceAdapter().getContactList(
				getSessionId(), query, orderBy, offset, maxResults);

		return contactsEntryList;
	}

	public String createContact(String firstName, String lastName,
			String email, String accountsId) throws Exception {

		String contactsId = super.createContact(firstName, lastName, email);
		if (contactsId != null && !contactsId.isEmpty()) {
			setRelationship(MODULE_CONTACTS, contactsId, MODULE_ACCOUNTS,
					accountsId);
		}

		return contactsId;
	}

	public ReturnAddContactList addContactList(List<Contact> contacts)
			throws Exception {
		return getServiceAdapter().addContactList(getSessionId(), contacts);
	}

	public ReturnExportContactList exportContactList() throws Exception {
		return getServiceAdapter().exportContactList(getSessionId());
	}
}
